//
//  ZZUIScrollViewInsetView.swift
//  TestZZBaseCustomsDemo
//
//  Created by Czz on 2023/1/16.
//

import UIKit
import ZZBaseCustoms
import ZZBase

class ZZUIScrollViewInsetView: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        

        
        view.addSubview(contentView)
        NSLayoutConstraint.activate([
            contentView.topAnchor.constraint(equalTo: view.topAnchor, constant: zz_navBarHeight),
            contentView.leftAnchor.constraint(equalTo: view.leftAnchor),
            contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            contentView.rightAnchor.constraint(equalTo: view.rightAnchor)
        ])
        
        let redView = UIView().zz_backgroundColor(.red)
        view.addSubview(redView)
        redView.translatesAutoresizingMaskIntoConstraints = false

        contentView.addSubview(redView)
        NSLayoutConstraint.activate([
            redView.topAnchor.constraint(equalTo: contentView.topAnchor),
            redView.leftAnchor.constraint(equalTo: contentView.leftAnchor),
            redView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            redView.rightAnchor.constraint(equalTo: contentView.rightAnchor),
            redView.widthAnchor.constraint(equalToConstant: zz_screen_width),
            redView.heightAnchor.constraint(equalToConstant: 10000)
        ])
        
        let yellowView = UIView().zz_backgroundColor(.yellow)
        redView.zz_addSubView(yellowView, constraints: [
            yellowView.leftAnchor.constraint(equalTo: redView.leftAnchor, constant: 20),
            yellowView.topAnchor.constraint(equalTo: redView.topAnchor, constant: 20),
            yellowView.widthAnchor.constraint(equalToConstant: 200),
            yellowView.heightAnchor.constraint(equalToConstant: 200)
        ])
        
        let alignments: [ZZUIButton.Alignment] = [.Center,.Left,.Top,.Right,.Bottom, .LeftCenter,.RightCenter,.TopCenter,.BottomCenter,.TopLeft,.TopRight,.BottomLeft,.BottomRight, ]
        var i = 0
        let btn = ZZUIButton().zz_backgroundColor(.systemOrange)
        btn.set(title: "发达fd", state: .normal)
            .set(image: .zz_named("icon-72"), state: .normal)
        
        var j = 0
        yellowView.zz_addTap { sender in
            
            if j >= alignments.count{  j = 0 }
            let a = alignments[j]
            ZZLog("imageAlignment  i = \(i) j = \(j)   \(a)")
            btn.imageAlignment(a)
            j += 1
        }
        
        btn.zz_addTap { sender in
            if i >= alignments.count{  i = 0 }
            let a = alignments[i]
            ZZLog("contentAlignment i = \(i) j = \(j)   \(a)")
//            btn.imageAlignment(a)
            btn.contentAlignment(a)
            i += 1
        }
        
        redView.zz_addSubView(btn, constraints: [
            btn.topAnchor.constraint(equalTo: yellowView.bottomAnchor, constant: 20),
            btn.centerXAnchor.constraint(equalTo: yellowView.centerXAnchor),
            btn.widthAnchor.constraint(equalToConstant: 200),
            btn.heightAnchor.constraint(equalToConstant: 200),
        ])
        
        let yellow2 = UIView().zz_backgroundColor(.yellow)
        redView.zz_addSubView(yellow2, constraints: [
            yellow2.leftAnchor.constraint(equalTo: yellowView.rightAnchor, constant: 20),
            yellow2.topAnchor.constraint(equalTo: yellowView.topAnchor),
            yellow2.widthAnchor.constraint(equalToConstant: 80),
            yellow2.heightAnchor.constraint(equalToConstant: 80)
        ])
        yellow2.zz_addTap { sender in
            let view = ZZDrawerView()
            view
                .topView(UIView().zz_backgroundColor(.systemGray4))
                .bottomView(UIView().zz_backgroundColor(.systemPink))
                .scrollView(UIScrollView())
                .topViewHeight(60)
                .bottomViewHeight(90)
                .superView(self.view)
                .delegate(self)
                .minHeight(0)
                .middleHeight(0)
                .maxHeight(zz_screen_height * 0.6)
            
            view.scrollView?.contentSize = CGSize(0, 1000)
            view.scrollView?.zz_backgroundColor(.systemCyan)
            
            view.show(state: .stretch, animation: true)
        }
        
        contentView.zz_topInsetView = topInsetView
        contentView.zz_topInsetViewHeight = 200
        contentView.zz_topInsetViewWidth = 200
        contentView.zz_topInsetViewIgnoreHeight = 40
        contentView.zz_topInsetViewBindingTop = true
        
        contentView.contentInsetAdjustmentBehavior = .never
        contentView.automaticallyAdjustsScrollIndicatorInsets = false
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    lazy var topInsetView: UIView = {
        let view = UIView()
        view.zz_backgroundColor(.blue)
        return view
    }()
    
    lazy var contentView: UIScrollView = {
        let view = UIScrollView().zz_backgroundColor(.orange)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

}

extension ZZUIScrollViewInsetView: ZZDrawerViewDelegate{
    func stateWillChange(drawerView: ZZDrawerView, state: ZZDrawerViewState) {
        
    }
    
    func stateChanged(drawerView: ZZDrawerView, state: ZZDrawerViewState) {
        
    }
    func heightChanged(drawerView: ZZDrawerView, height: CGFloat) {
        
    }
}
